package com.tjsinfo.backworkflow.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tjsinfo.backworkflow.dao.CtpCommentAllMapper;
import com.tjsinfo.backworkflow.dao.EdocOpinionMapper;
import com.tjsinfo.backworkflow.dao.EdocSummaryMapper;
import com.tjsinfo.backworkflow.vo.CtpCommentAll;
import com.tjsinfo.backworkflow.vo.EdocOpinion;
import com.tjsinfo.backworkflow.vo.EdocSummary;
import tk.mybatis.mapper.entity.Example;

@Service
public class BackWorkFlowService implements IBackWorkFlowService {

	@Autowired
	private EdocSummaryMapper  edocSummaryMapper;
	@Autowired
	private EdocOpinionMapper  edocOpinionMapper;
	
	@Autowired
	private CtpCommentAllMapper  commentAllMapper;
	
	
	@Override
	public List<EdocSummary> selectTitle( String title) {
		// TODO Auto-generated method stub
		List<EdocSummary> list = edocSummaryMapper.selectSummary("'%"+title+"%'");		 
		return list;
	}

	@Override
	public List<EdocOpinion> selectOpinion(Long id) {
		// TODO Auto-generated method stub
		return edocOpinionMapper.selectOpinion(id);		 
	}

	@Transactional
	@Override
	public boolean updateOpinion(String opinionId, String content) {
		// TODO Auto-generated method stub
		EdocOpinion edocOpinion = edocOpinionMapper.selectByPrimaryKey(opinionId);
		edocOpinion.setContent(content);
		edocOpinionMapper.updateByPrimaryKey(edocOpinion);
		CtpCommentAll ctpCommentAll = new CtpCommentAll();
		ctpCommentAll.setAffairId(edocOpinion.getAffairId().toString());
		ctpCommentAll.setModuleId(edocOpinion.getEdocId().toString());
		ctpCommentAll = commentAllMapper.selectOne(ctpCommentAll);
		ctpCommentAll.setContent(content);
		if (commentAllMapper.updateByPrimaryKey(ctpCommentAll) > 0 ) {
			return true;
		} else {
			return false;
		}
		
		
	}

	@Transactional
	@Override
	public boolean deleteOpinion(String opinionId) {
		// TODO Auto-generated method stub
		EdocOpinion edocOpinion = edocOpinionMapper.selectByPrimaryKey(opinionId);
		Example CtpCommentAllExample = new Example(CtpCommentAll.class);
		CtpCommentAllExample.createCriteria().andEqualTo("affairId", edocOpinion.getAffairId())
		.andEqualTo("moduleId", edocOpinion.getEdocId());
		if(edocOpinionMapper.deleteByPrimaryKey(opinionId) > 0 && 
			commentAllMapper.deleteByExample(CtpCommentAllExample) > 0){
			return true;
		} else {
			return false;
		}
	}

	@Override
	public EdocOpinion selectOneOpinion(Long opinionId) {
		// TODO Auto-generated method stub
		EdocOpinion edocOpinion = edocOpinionMapper.selectByPrimaryKey(opinionId.toString());
		return edocOpinion;
	}

}
